package com.ydd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.LoginAndRegDTO;
import com.ydd.dto.UserDTO;
import com.ydd.dto.YDDUserPageQueryDTO;
import com.ydd.entity.YddUser;
import com.ydd.mapper.YddUserMapper;
import com.ydd.result.PageResult;
import com.ydd.service.YddUserService;
import com.ydd.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.unit.DataUnit;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @Description TODO
 * @Author fang
 * @Date 2023-05-29 16:32
 */
@Service
public class YddUserServiceImpl implements YddUserService {
    @Autowired
    private YddUserMapper yddUserMapper;
    @Override
    public PageResult page(YDDUserPageQueryDTO yddUserPageQueryDTO) {
        PageHelper.startPage(yddUserPageQueryDTO.getPage(),yddUserPageQueryDTO.getLimit());
        Page<UserVO> userVOList = yddUserMapper.page(yddUserPageQueryDTO);
        return new PageResult(userVOList.getTotal(),userVOList.getPages(),
                userVOList.getPageSize(),yddUserPageQueryDTO.getPage(),userVOList.getResult());
    }

    /**
     * 查询用户详情
     * @param id
     * @return
     */
    @Override
    public UserVO roInfo(String id) {
        UserVO user = yddUserMapper.rowInfo(id);
        user.setPassword("");
        return user;
    }

    @Override
    public void add(UserDTO userDTO) {
        String password = DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes());
        userDTO.setPassword(password);
        yddUserMapper.add(userDTO);
    }

    @Override
    public void del(String id) {
        yddUserMapper.del(id);
    }

    @Override
    public void update(UserDTO userDTO) {
        String password = DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes());
        userDTO.setPassword(password);
        yddUserMapper.userUpdate(userDTO);
    }

    /**
     * 根据条件查询YDD用户
     * @param loginAndRegDTO
     * @return
     */
    @Override
    public YddUser findByCondition(LoginAndRegDTO loginAndRegDTO) {
        String md5 = DigestUtils.md5DigestAsHex(loginAndRegDTO.getPassword().getBytes());
        loginAndRegDTO.setPassword(md5);
        YddUser yddUser = yddUserMapper.findByCondition(loginAndRegDTO);
        return yddUser;
    }

}
